$.blockUI.defaults.css.border = 'none';
$.blockUI.defaults.css.padding = '15px';
$.blockUI.defaults.css.backgroundColor = '#000';
$.blockUI.defaults.css.opacity = '0.6';
$.blockUI.defaults.css.color = '#fff';
//$.blockUI.defaults.css.message = '<h1><img src="./busy.gif" /> Loading...</h1>';
$.blockUI.defaults.css.timeout = '1000';

function clear(){
	var s3='';
	$("#userLookUpTable").empty();
	$("#userId").val(s3);
	$("#userName").val(s3);
	$("#role").remove();
}

function userlookup(){
	$.blockUI();
	
	$.post('userLookUpPage.htm',{data:'data'},function(data){
		$("#userLookUpFiltersDiv").empty().html(data);
		$.unblockUI();
	},"html");
}

function createUserLookUpDialog(){
	userLookUpDialog = $("#userLookUpFiltersDiv").dialog ({
		modal: true,
		autoOpen: false,
		title: "User Lookup",
		height: 300,
		width: 1000,
		show: "explode",
		hide: "explode",
		resizable: false,
		position: ['middle','middle'],
		dialogClass : 'dialogClass userLookUpDialog',
		closeOnEscape: false,
		close: function(event,ui){
			clear();
		}
	});
}

function showUserLookUpDialog(){
	if(!userLookUpDialog.dialog("isOpen")){
		$("#searchForm").append("<input type='hidden' value='' id='role' name='role'>");
		userLookUpDialog.dialog('open');
	}
	else{
		userLookUpDialog.dialog("moveToTop");
	}
	
}

function showUserLookUpDialogForWF(role){

	if(!userLookUpDialog.dialog("isOpen")){
		$("#searchForm").append("<input type='hidden' value='"+ role +"' id='role' name='role'>");
		userLookUpDialog.dialog('open');
	}
	else{
		userLookUpDialog.dialog("moveToTop");
	}
}

function submitSearchUserDetails(String){
	var role = $("#role").val();
//	alert(role);
	var valueArray = $("#userSubmit :checked").val().toString().split(",");
	
	var liId = valueArray[0];
    var liText =valueArray[1];
    
//    var checkLiId = document.getElementById(liId);
    var checkLiId = $("#authUserId > #"+liId).length;
    if(checkLiId==0){
    	$("#authUserId").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
    	$("#authUserId").append("<input id='"+ liId +"' type='hidden' name='authId' value='"+ liId +"'>");
    }
    else{
    	alert("User With Name "+liText+" Already Selected For Authorized Signatory");	
    }	
    userLookUpDialog.dialog("close");
    
	if(role == 'ROLE_AUTHSIGN'){
		$("#authSignId").val(valueArray[0]);
		$("#authSign").val(valueArray[1]);
		userLookUpDialog.dialog("close");
		return;
	}
	
	if(role == 'ROLE_APPROVER'){
		$("#controller").val(valueArray[0]);
		userLookUpDialog.dialog("close");
	}
	if(role == ""){
		$("#ownerName").val(valueArray[1]);
		$("#docOwnerId").val(valueArray[0]);
		userLookUpDialog.dialog("close");
	}
	if(role == 'ROLE_REVIEWER'){
		$("#reviewer").val(valueArray[0]);
	}
	else{
		$("#approver").val(valueArray[0]);
	}
		$("#userIdResult").val(valueArray[0]);
		$("#userNameResult").val(valueArray[1]);
		$("#locationNameResult").val(valueArray[2]); 
		$("#departmentNameResult").val(valueArray[3]);
		$("#locationName").val(valueArray[4]); 
		$("#departmentName").val(valueArray[5]);
		$("#owner").val(valueArray[0]);
		$("#documentOwner").val(valueArray[0]);
}

function closeUserLookUpDialog(){
	if(userLookUpDialog && userLookUpDialog.dialog("isOpen")){
		userLookUpDialog.dialog("close");
	}
}

function generateListOfUsers(){
	$.blockUI();
	
	var searchData = $("#searchForm").formSerialize();
	$.post("userLookUpPage1.htm",searchData,function(data){
		 $("#userLookUpTable").empty().html(data);
		 $.unblockUI();
	},"html");

	
}


function appropriateView(){
	$.blockUI();
	
	$.post('selectView.htm',{data:'data'},function(data){
		$("#outerContentDiv").empty().html(data);
		$.unblockUI();
		$.blockUI();
		$.post('loadDashBoard.htm',{data:'data'},function(data){
			$("#contentDiv").empty().html(data);
			$.unblockUI(); 
		},"html");
	},"html");
}


function documentlookup(){
	$.blockUI();
	
	$.post('documentLookUpPage.htm',{data:'data'},function(data){
		$("#documentLookUpFiltersDiv").empty().html(data);
		$.unblockUI();
	},"html");
}



function cleardoc(){
	var s3='';
	$("#documentLookUpTable").empty();
	$("#documentName").val(s3);
	
}

function createDocumentLookUpDialog(){
	
	documentLookUpDialog = $("#documentLookUpFiltersDiv").dialog ({
		modal: true,
		autoOpen: false,
		title: "Document Lookup",
		height: 350,
		width: 1000,
		show: "explode",
		hide: "explode",
		resizable: false,
		position: ['middle',50],
		dialogClass : 'dialogClass documentLookUpDialog',
		closeOnEscape: false,
		close: function(event,ui){
			cleardoc();		
		}
	});
}

function closeDocumentLookupDialog(){
	if(documentLookUpDialog && documentLookUpDialog.dialog("isOpen"))
		documentLookUpDialog.dialog("close");
}

function showDocumentLookUpDialog(){
		
	if(!documentLookUpDialog.dialog("isOpen")){
		documentLookUpDialog.dialog('open');
		documentLookUpDialog.dialog('option','height',300);
	}
	else{
		documentLookUpDialog.dialog("moveToTop");
	}
	
}

function setRequestingPageForDocLookup(reqPageName){
	$("#docLookupRequestingPage").attr("value",reqPageName);
	showDocumentLookUpDialog();
}

function updateDocumentLookupFilters(functionalityName){
	$.blockUI();
	$.post('documentLookUpPage.htm',{functionalityName:functionalityName},function(data){
		$("#documentLookUpFiltersDiv").empty().html(data);
		$.unblockUI();
		setRequestingPageForDocLookup('restricted');
	},"html");
}

function showListOfAvailableDocuments(){
	$.blockUI();
	
	var searchData = $("#searchDocForm").formSerialize();
	
	$.post("documentLookUpPage1.htm",searchData,function(data){
		 $("#documentLookUpTable").empty().html(data);
		 $.unblockUI();
	},"html");
	
}
function searchDocuments(){
	$.blockUI();
	
	var url = getWorkFlowURL();
	var formData = $("#searchCriteria").formSerialize();
	
	formData = formData+"&ajaxSource=ajaxRequest&mode=embedded&_eventId=search";
	$.post(url,formData,function(data,response){
		$("#searchResult").empty().html(data);
		$.unblockUI();
	});
}
function submitSearchDocumentDetails(String){

	var valueArray = $("#docSubmit :checked").val().toString().split(",");
	
	$("#searchedDocName").val(valueArray[0]);
	$("#searchDocTypeID").val(valueArray[6]);
	$("#searchDocCatID").val(valueArray[7]); 
	$("#searchDocSubCatID").val(valueArray[8]);
	$("#searchDocId").val(valueArray[1]);
	var docStatus = valueArray[11];
	docStatus = $.trim(docStatus);
	if(docStatus.indexOf('Active', 0) != -1){
		$("#activateButton").attr("disabled", "disabled");
		$("#deActivateButton").removeAttr("disabled");
	}
	else if(docStatus.indexOf('Deactive', 0) != -1){
		$("#deActivateButton").attr("disabled", "disabled");
		$("#activateButton").removeAttr("disabled");
	}else{
		$("#activateButton").attr("disabled", "disabled");
		$("#deActivateButton").attr("disabled", "disabled");
//		alert("Please select a document with status Active or Deactive.\n You have selected a document with status " + docStatus);
	}
	
	
		var liId = valueArray[1];
        var liText =valueArray[0];
        
        //var checkLiId = document.getElementById(liId);
        var checkLiId = $("#"+liId).length;
        if(checkLiId==0){
        	$("#linkedDocId").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
        	$("#linkedDocId").append("<input id='"+ liId +"' type='hidden' name='linkedDocId' value='"+ liId +"'>");
        }
        else{
        	alert("Document With Name "+liText+" Already Selected For Linking");	
        }
        documentLookUpDialog.dialog("close");
}

function deleteDocument(id)
{
    $("#linkedDocId > li.ui-selected").each(function(index){
        var liId = $(this).attr('id');
        $(this).remove();
        $("#"+liId).remove();
    });	
}
function selectroleDialog(){
	$.blockUI();
	
	$.post('selectRole.htm',{data:'data'},function(data){
		$("#selectRole").empty().html(data);
		$.unblockUI();
	},"html");
	selectRoleDialog.dialog('open');
}

function createSelectRoleDialog(){
	selectRoleDialog = $("#selectRole").dialog({
		autoOpen: false,
		title: "Select Role",
		height: 'auto',
		width: 'auto',
		modal: true,
		resizable: false,
		dialogClass : 'dialogClass selectRoleDialog'
	});
}

function showSelectRoleDialog(){
	
	if(!selectRoleDialog.dialog("isOpen")){
		selectRoleDialog.dialog('close');
	}
	selectRoleDialog.dialog('open');
}

function getSelectedRole(){
	
	var selectedRole = $("#selectRoleToLogin :checked").val().toString();
	$.post('appropriateUser/'+selectedRole+'.htm',{data:'data'},function(data){
		$("#outerContentDiv").empty().html(data);
	},"html");
	selectRoleDialog.dialog("close");
	
}

function createMainLayout(){
	appMainLayout = $('#mainLayoutBody').layout({
		north__size: 100,
		north__closable: false,
		north__resizable: false,
		north__spacing_open:0,
		north__spacing_close:0,
		north__paneSelector: '#headerDiv',
		south__size:20,
		south__resizable:false,
		south__spacing_open:0,
		south__spacing_close:0,
		south__paneSelector: '#footerDiv',
		center__paneSelector: '#outerContentDiv'
	});
	createChangePwdDialog();
	/*loadingDialog = $("#showLoadingDiv").dialog({
		autoOpen: false,
		height: 135,
		width: 110,
		modal: true,
		resizable: false,
		dialogClass : 'loadingDialogClass',
		closeOnEscape : false
	});
	loadingText = $("#customLoadingMessage");
	loadingDialog.dialog('widget').find(".ui-dialog-titlebar").hide();*/
	/*appInnerLayout = $('#outerContentDiv').layout({
		north__size: 'auto',
		north__closable: false,
		north__resizable: false,
		north__spacing_open:0,
		north__spacing_close:0,
		north__paneSelector: '#menuDiv',
		west__paneSelector: '#contentDiv'
	});*/
	appMainLayout.resizeAll();
	//appInnerLayout.resizeAll();
}
function updateDocumentLink(formId)
{
	$.blockUI();
	
	var url = getWorkFlowURL();
	var formData = $("#"+formId).formSerialize();
	formData = formData+"&ajaxSource=ajaxRequest&mode=embedded&_eventId=updateDocumentLink";
	
	$.post(url,formData,function(data,response){
		if(response == "success")
			alert("Successfully Done.");
		$.unblockUI(); 
	});	
	
}
function saveAuthSignatory(formId)
{
	$.blockUI();
	
	var url = getWorkFlowURL();
	var formData = $("#"+formId).formSerialize();
	formData = formData+"&ajaxSource=ajaxRequest&mode=embedded&_eventId=assignAuthSignatory";
	
	$.post(url,formData,function(data,response){
		$.unblockUI(); 
	});	
}

function createSelectRoleDialog(){
	selectRoleDialog = $("#selectRole").dialog({
		autoOpen: false,
		height: 'auto',
		width: 250,
		modal: true,
		resizable: false,
		dialogClass : 'dialogClass selectRoleDialog',
		title: "Select Role"
	});
}

function showSelectRoleDialog(){
	if(!selectRoleDialog.dialog("isOpen")){
		selectRoleDialog.dialog('close');
	}
	selectRoleDialog.dialog('open');
}

function createChangePwdDialog(){
	changePwdDialog = $("#changePwdDiv").dialog({
		autoOpen: false,
		height: 'auto',
		width: 400,
		modal: true,
		resizable: false,
		dialogClass : 'dialogClass changePwdDialog',
		buttons : {
			"Change Password" : function(){
				var newPwd = $("#newPassword").val();
				var cfmNewPwd = $("#confirmNewPassword").val();
				
				if(newPwd == ""){
					alert("Enter new password");
				}
				else if(cfmNewPwd == ""){
					alert("Enter confirm password.");
				}
				else if(newPwd == cfmNewPwd){
					$.blockUI();
					$.post(contextRoot+"changePwdDialog.htm",{'password':newPwd},function(data,response){
						alert(data);
						changePwdDialog.dialog('close');
						$.unblockUI(); 
					});
				}
				else {
					alert("New Password and Confirm New Password should be same.\n Try Again!!");
					$("#newPassword").val('');
					$("#confirmNewPassword").val('');
					$("#newPassword").focus();
				}
			},
			"Cancel" : function(){
				changePwdDialog.dialog('close');
			}
		}
	});
}

function showChangePwdDialog(){
	if(!changePwdDialog.dialog("isOpen")){
		changePwdDialog.dialog('close');
	}
	changePwdDialog.dialog('open');
	$("#newPassword").focus();
	//changePwdDialog.dialog('moveToTop');
}

/*function showLoadingDialog(message){
	if(!loadingDialog.dialog("isOpen")){
		loadingDialog.dialog("open");
	}
	loadingText.empty();
	loadingText.html(message);
	loadingDialogTimeout = setTimeout('hideLoadingDialogAfterTimeout()',10000);
}

function showLoadingDialogWithTimeout(message,timeout){
	if(!loadingDialog.dialog("isOpen")){
		loadingDialog.dialog("open");
	}
	loadingText.empty();
	loadingText.html(message);
	loadingDialogTimeout = setTimeout('hideLoadingDialogAfterTimeout()',timeout);
}

function hideLoadingDialogAfterTimeout(){
	if(loadingDialog.dialog("isOpen")){
		loadingDialog.dialog("close");
	loadingDialogTimeout = null;
	alert("The request has not completed within defined time interval.\nPlease try again. " +
			"If problem persists, report it to Administrator for support.");
	}
}

function hideLoadingDialog(){
//	alert(loadingDialogTimeout);
	clearTimeout(loadingDialogTimeout);
//	loadingDialogTimeout = null;
	if(loadingDialog.dialog("isOpen")){
		loadingDialog.dialog("close");
	}
}*/

function goBack(eventName){
	$.blockUI();
	
	var url = getWorkFlowURL();
	
	$.post(url,{ajaxSource:'ajaxRequest',_eventId:eventName,mode:'embedded'},function(data){
		$(mainContentDiv).empty().html(data);
		//hideLoadingDialog();
		$.unblockUI();
	});
}